﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using SharePoint.Contrib.Extensions;
using System.Xml.Linq;
using System.Xml;

namespace NUnit.SharePointContrib
{
    [TestFixture]
    public class XAttributeExtensionsTest
    {
        XElement xml;

        [SetUp]
        public void Init()
        {
            xml = new XElement("elements",
                    new XElement("element", "First", new XAttribute("element", "First")),
                    new XElement("element", "10", new XAttribute("element", "10")),
                    new XElement("element", "2010-02-02", new XAttribute("element", "2010-02-02")));

       }

        [TearDown]
        public void Dispose()
        {
        }

        [Test]
        public void CanConvertAttributeStringValue()
        {
            var element = (from x in xml.Descendants("element").Attributes()
                           where x.Value == "First"
                           select x).FirstOrDefault();

            Assert.That(element.OfType<string>(), Is.EqualTo("First"));
        }

        [Test]
        public void CanConvertAttributeIntegerValue()
        {
            var element = (from x in xml.Descendants("element").Attributes()
                           where x.Value == "10"
                           select x).FirstOrDefault();

            Assert.That(element.OfType<int>(), Is.EqualTo(10));
        }

        [Test]
        public void CanConvertAttributeDateTimeValue()
        {
            var element = (from x in xml.Descendants("element").Attributes()
                           where x.Value == "2010-02-02"
                           select x).FirstOrDefault();

            Assert.That(element.OfType<DateTime>(), Is.EqualTo(new DateTime(2010, 2, 2)));
        }






    }
}
